package org.forten.swt;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

import org.apache.commons.io.FileUtils;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

public class StudentWin {

	protected Shell shell;
	private Text nameText;
	private Text ageText;
	private Label label_1;
	private Text telText;
	private Label label_2;
	private Button button;

	/**
	 * Launch the application.
	 * 
	 * @param args
	 */
	public static void main(String[] args) {
		try {
			StudentWin window = new StudentWin();
			window.open();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Open the window.
	 */
	public void open() {
		Display display = Display.getDefault();
		createContents();
		shell.open();
		shell.layout();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
	}

	/**
	 * Create contents of the window.
	 */
	protected void createContents() {
		shell = new Shell();
		shell.setSize(420, 236);
		shell.setText("SWT Application");

		Label label = new Label(shell, SWT.NONE);
		label.setAlignment(SWT.RIGHT);
		label.setBounds(62, 37, 61, 17);
		label.setText("姓名：");

		nameText = new Text(shell, SWT.BORDER);
		nameText.setBounds(129, 34, 222, 23);

		ageText = new Text(shell, SWT.BORDER);
		ageText.setBounds(129, 63, 222, 23);

		label_1 = new Label(shell, SWT.NONE);
		label_1.setText("年龄：");
		label_1.setAlignment(SWT.RIGHT);
		label_1.setBounds(62, 66, 61, 17);

		telText = new Text(shell, SWT.BORDER);
		telText.setBounds(129, 92, 222, 23);

		label_2 = new Label(shell, SWT.NONE);
		label_2.setText("电话：");
		label_2.setAlignment(SWT.RIGHT);
		label_2.setBounds(62, 95, 61, 17);

		button = new Button(shell, SWT.NONE);
		button.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				String name = nameText.getText();
				String tel = telText.getText();
				String ageStr = ageText.getText();
				int age = Integer.parseInt(ageStr);
				Student s = new Student(name, age, tel);

				try (ByteArrayOutputStream baos = new ByteArrayOutputStream();
						ObjectOutputStream oos = new ObjectOutputStream(baos);) {
					oos.writeObject(s);
					oos.flush();
					byte[] contents = baos.toByteArray();
					FileUtils.writeByteArrayToFile(new File("g:/student.ser"),
							contents);
				} catch (IOException ex) {
					ex.printStackTrace();
				}
			}
		});
		button.setBounds(129, 161, 80, 27);
		button.setText("保存");

		Button button_1 = new Button(shell, SWT.NONE);
		button_1.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				ObjectInputStream ois = null;
				try {
					byte[] contents = FileUtils.readFileToByteArray(new File(
							"g:/student.ser"));
					ois = new ObjectInputStream(new ByteArrayInputStream(
							contents));
					Student s = (Student) ois.readObject();
					nameText.setText(s.getName());
					ageText.setText("" + s.getAge());
					telText.setText(s.getTel());
				} catch (IOException ex) {
					throw new IllegalArgumentException("反序列化对象失败", ex);
				} catch (ClassNotFoundException ex) {
					throw new IllegalStateException("对象类型未找到，反序列化对象失败", ex);
				} finally {
					try {
						ois.close();
					} catch (IOException e1) {
						e1.printStackTrace();
					}
				}
			}
		});
		button_1.setBounds(218, 161, 80, 27);
		button_1.setText("读出");

	}
}
